package com.inspur.dgov.vlbod.controller;

import com.inspur.dgov.vlbod.entity.DemandCollect;
import com.inspur.dgov.vlbod.http.DgovResponse;
import com.inspur.dgov.vlbod.secure.token.UserRecord;
import com.inspur.dgov.vlbod.service.DemandCollectService;
import com.inspur.dgov.vlbod.service.UserRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 点播收藏服务控制器
 * </p>
 *
 * @author chiyupei
 * @since 2023-05-29
 */
@Slf4j
@RestController
@RequestMapping("/demandCollect")
public class DemandCollectController {

    @Autowired
    private DemandCollectService demandCollectService;

    @Autowired
    private UserRecordService userRecordService;

    /**
     * 保存点播收藏
     *
     * @param request  request
     * @param demandId 点播id
     * @return 结果
     */
    @GetMapping("/add/{id}")
    public DgovResponse saveCollect(HttpServletRequest request, @PathVariable("id") String demandId) {
        // 获取当前登录用户ID
        UserRecord userRecord = this.userRecordService.getUserRecord(request);
        String userId = userRecord.getUserId();
        try {
            boolean demandCollect = demandCollectService.saveCollect(demandId, userId);
            return DgovResponse.success(demandCollect);
        } catch (Exception exception) {
            log.error("save collet failed", exception);
            return DgovResponse.fail(exception.getMessage());
        }
    }

    /**
     * 删除收藏
     *
     * @param request  request
     * @param demandId 点播id
     * @return 结果
     */
    @GetMapping("/delete/{id}")
    public DgovResponse removeCollect(HttpServletRequest request, @PathVariable("id") String demandId) {
        UserRecord userRecord = this.userRecordService.getUserRecord(request);
        String userId = userRecord.getUserId();
        try {
            demandCollectService.deleteCollect(demandId, userId);
        } catch (Exception exception) {
            log.error("remove collect failed", exception);
            return DgovResponse.fail(exception.getMessage());
        }
        return DgovResponse.success();
    }

    /**
     * 获取点播收藏总数
     *
     * @param demandId 点播id
     * @return 总数
     */
    @GetMapping("/count/{id}")
    public DgovResponse getCollectCount(@PathVariable("id") String demandId) {
        try {
            return DgovResponse.success(demandCollectService.getCollectCount(demandId));
        } catch (Exception exception) {
            log.error("count collect failed", exception);
            return DgovResponse.fail(exception.getMessage());
        }
    }

    /**
     * 判断是否收藏过
     *
     * @param request
     * @param demandId
     * @return
     */
    @GetMapping("/check/{id}")
    public DgovResponse isCollect(HttpServletRequest request, @PathVariable("id") String demandId) {
        UserRecord userRecord = this.userRecordService.getUserRecord(request);
        String userId = userRecord.getUserId();
        try {
            return DgovResponse.success(demandCollectService.isCollect(demandId, userId));
        } catch (Exception exception) {
            log.error("check exist collect failed", exception);
            return DgovResponse.fail(exception.getMessage());
        }
    }

}
